package com.codefox.controller;



import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.codefox.entity.TbUser;
import com.codefox.service.TbUserService;
import com.codefox.util.MD5Util;

/**
 * 前台登录控制器
 *
 * @author xiaobiye
 * @author litao
 * @author puma
 * @date 2018年9月28日21:18:14
 */
@Controller
@RequestMapping("/front")
public class loginController {

	private static final String PASS = "y";
	private static final String FAIL = "n";

	private final TbUserService tbUserService;

	@Autowired
	public loginController(TbUserService tbUserService) {
		this.tbUserService = tbUserService;
	}

	/**
	 * 用于点击登录时处理
	 */
	@RequestMapping("/loginAction")
	public ModelAndView selectName(@RequestParam("username") String username, @RequestParam("password") String password,
			@RequestParam("code") String code, HttpServletRequest req,HttpServletResponse response) {
		System.out.println("已进入loginAction后台处理");
		StringBuffer message = new StringBuffer();
		System.out.println(username + password);
		password = MD5Util.encrypt(password);
//		password.(MD5Util.encrypt(password);
		System.out.println(password);
		TbUser user = tbUserService.login(username, password, message);

		HttpSession session = req.getSession();
		String sessionVcode = (String) session.getAttribute("vcode");

		req.getSession().setAttribute("name", username);

		System.out.println("msg:" + message);

		System.out.println("sessionVcode:" + sessionVcode);
		System.out.println("vcode:" + code);

		System.out.println(code.equals(sessionVcode));
		if (!code.equalsIgnoreCase(sessionVcode)) {
			message.delete(0, message.length());
			message.append("验证码错误");
			System.out.println("message123:" + message);
			return new ModelAndView("forward:login.jsp", "msg", message);
		} else if (user != null) {
			// 登录方法返回对象，表示登录成功
			String uName = (String) req.getSession().getAttribute("name");
			System.out.println("uName:" + uName);
			Cookie cookie = new Cookie("userName", uName);
			cookie.setMaxAge(30*24*60*60);
			cookie.setPath(req.getContextPath() + "/");
			response.addCookie(cookie);
			
			return new ModelAndView("redirect:/front/index.html?adminName=" + uName);
		} else {
			return new ModelAndView("forward:login.jsp", "msg", message);
		}
	}

	/**
	 * 用于ajax 验证用户名是否正确
	 */
	@ResponseBody
	@RequestMapping("/loginName")
	public String selectName(@RequestParam("username") String username) {
		/* System.out.println(username); */
		String user = tbUserService.checkUserName(username);
		/* System.out.println("loginName-user:"+user); */
		if (PASS.equals(user)) {
			return user;
		}
		return FAIL;
	}

	/**
	 * 注册处理流程
	 * 
	 * @param tbUser 用户对象
	 * @param code 验证码
	 * @param req request对象
	 * @return 模型和视图
	 */
	@RequestMapping("/adduser")
	public ModelAndView registerDo(TbUser tbUser, @RequestParam("code") String code, HttpServletRequest req) {
		System.out.println("已进入注册页面" + tbUser);
		StringBuffer message = new StringBuffer();
		HttpSession session = req.getSession();
		String sessionVcode = (String) session.getAttribute("vcode");
		tbUser.setPassword(MD5Util.encrypt(tbUser.getPassword()));
		String isExist = tbUserService.checkUserName(tbUser.getUserName());
		if (PASS.equals(isExist)) {
			message.append("用户名已存在");
			return new ModelAndView("forward:register.jsp", "message", message);
		} else if (!code.equalsIgnoreCase(sessionVcode)) {
			message.delete(0, message.length());
			message.append("验证码错误");
			return new ModelAndView("forward:register.jsp", "message", message);
		} else {
			boolean isAdd = tbUserService.register(tbUser);
			if (isAdd) {
				//JOptionPane.showMessageDialog(null, "注册成功！正在前往登录页面", "注册成功", JOptionPane.WARNING_MESSAGE);
				message.delete(0, message.length());
				message.append("注册成功，请登录！");
				return new ModelAndView("forward:login.jsp", "msg", message);
			} else {
				//JOptionPane.showMessageDialog(null, "注册 失败！正在前往注册页面", "注册失败", JOptionPane.WARNING_MESSAGE);
				
				return new ModelAndView("forward:register.jsp");
			}
		}
	}
}
